package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysUserMapper;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolCourseSectionResourceCommentQuery;
import cn.ljy.uem.service.SchoolCourseSectionResourceCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * ischool教学章节资源评论实现
 *
 * @author 王秋林
 * @date 2022/09/15
 */
@Service
@Slf4j
public class ISchoolCourseSectionResourceCommentService
        extends IBaseService<SchoolCourseSectionResourceComment, SchoolCourseSectionResourceCommentQuery>
        implements SchoolCourseSectionResourceCommentService {
    @Autowired
    private SchoolCourseSectionResourceCommentMapper mapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SchoolCourseSectionResourceMapper schoolCourseSectionResourceMapper;


    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(SchoolCourseSectionResourceComment schoolCourseSectionResourceComment) {
        if (mapper.findById(schoolCourseSectionResourceComment.getId()) == null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeSave(SchoolCourseSectionResourceComment comment) {
        if (comment.getSort() == null){
            comment.setSort(100);//设置默认排序
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }
    @Override
    protected void afterGet(SchoolCourseSectionResourceComment comment) {

        //查找账户信息
        if (comment.getUserId() != null) {
            //查找账户信息
            //赋值
            comment.setSysUser(sysUserMapper.findById(comment.getUserId()));
        }
        //教学资源任务关联属性
        if (comment.getSectionResourceId() != null) {
            //教学资源任务信息
            //赋值
            comment.setSchoolCourseSection(schoolCourseSectionResourceMapper.findById(comment.getSectionResourceId()));
        }
    }

    @Override
    protected void afterFindAll(List<SchoolCourseSectionResourceComment> list) {
        for (SchoolCourseSectionResourceComment comment : list) {

            //添加账户关联属性
            //查找账户信息
            if (comment.getUserId() != null) {
                //查找账户信息
                //赋值
                comment.setSysUser(sysUserMapper.findById(comment.getUserId()));
            }
            //教学资源任务关联属性
            if (comment.getSectionResourceId() != null) {
                //教学资源任务信息
                //赋值
                comment.setSchoolCourseSection(schoolCourseSectionResourceMapper.findById(comment.getSectionResourceId()));
            }
        }
    }
}
